using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Admin2022Soft2.Application.Common.Interface;

namespace Admin2022Soft2.Api.Service
{
    public class SessionUserService : ISessionUserService
    {
        private readonly IHttpContextAccessor _contextAccessor;

        public SessionUserService(IHttpContextAccessor contextAccessor)
        {
            _contextAccessor = contextAccessor;
        }

        public Guid? UserId
        {
            get
            {
                // 从token中拿到对应的值
                var userIdString = _contextAccessor.HttpContext?.User.FindFirstValue("UserId");
                if (userIdString == null)
                {
                    return null;
                }
                else
                {
                    var userIdGuid = new Guid(userIdString);
                    return userIdGuid;
                }

            }

        }

        public string? Username
        {
            get
            {
                // 从token中拿到对应的值
                var username = _contextAccessor.HttpContext?.User.FindFirstValue("UserName");
                return username;
            }
        }
    }
}